package com.ysstech.kettle.controller;


import java.io.File;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ysstech.common.annotation.YssLog;
import com.ysstech.common.annotation.YssPermissions;
import com.ysstech.common.controller.BaseController;
import com.ysstech.common.exception.BusinessException;
import com.ysstech.common.util.StringUtil;
import com.ysstech.common.util.fileutil.FileWriterUtil;
import com.ysstech.common.util.resultutil.RestResult;
import com.ysstech.common.util.resultutil.ResultGenerator;
import com.ysstech.kettle.entity.JobRecord;
import org.apache.commons.io.FileUtils;
import com.ysstech.kettle.service.JobRecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.ResponseBody;

/**
 * <p>
 * 转换执行记录表 前端控制器
 * </p>
 *
 * @author lishuangliang
 * @since 2021-06-04
 */
@Controller
@Slf4j
@Api(tags = "转换执行记录api")
@RequestMapping("/jobRecord")
public class JobRecordController extends BaseController {
    @Autowired
    private JobRecordService jobRecordService;

    @YssPermissions(value = {"kettle:job:record"})
    @ApiOperation(value = "跳转转换监控管理页面")
    @YssLog(module = "转换监控管理", remark = "跳转转换监控管理页面")
    @RequestMapping(value = "/page")
    public String page() throws Exception {
        return "kettle/jobRecord";
    }

    /**
     * 获取作业监控管理表信息
     *
     * @return
     */
    @YssPermissions(value = {"kettle:job:record"})
    @ApiOperation(value = "获取转换执行记录信息")
    @YssLog(module = "转换执行记录", remark = "获取转换执行记录信息")
    @PostMapping(value = "/listInfo")
    @ResponseBody
    public RestResult listInfo(JobRecord jobRecord, String page, String rows) throws Exception {
        return ResultGenerator.getSuccessResult("获取转换执行记录信息成功",
                jobRecordService.queryAllTransRecord(jobRecord, this.getPage(page, rows)));
    }

    /**
     * 获取作业日志记录信息
     *
     * @return
     */
    @ApiOperation(value = "获取转换执行记录信息")
    @YssLog(module = "转换执行记录", remark = "获取转换执行记录信息")
    @PostMapping(value = "/getJobRecord")
    @ResponseBody
    public RestResult getJobRecord(JobRecord jobRecord) throws Exception {
        QueryWrapper<JobRecord> wrapper = new QueryWrapper<>();
        if (null == jobRecord || null == jobRecord.getSelFtype() || StringUtils.isBlank(jobRecord.getJobId())) {
            throw new BusinessException("获取作业任务日志信息失败，请稍后重试！");
        }
        wrapper.eq("FTYPE", jobRecord.getSelFtype());
        wrapper.eq("JOB_ID", jobRecord.getJobId());
        wrapper.orderByDesc("STOP_TIME");
        List<JobRecord> list = jobRecordService.list(wrapper);
        return ResultGenerator.getSuccessResult("获取转换执行记录信息成功",
                !CollectionUtils.isEmpty(list) ? list.get(0) : null);
    }


    /**
     * 删除参数管理信息
     *
     * @return
     */
    @YssPermissions(value = {"kettle:job:param:del"})
    @ApiOperation(value = "删除转换执行信息")
    @YssLog(module = "转换执行记录", remark = "删除转换执行信息")
    @RequestMapping(value = "/delJobRecord")
    @ResponseBody
    public RestResult delJobRecord(String ids) throws Exception {
        if (StringUtils.isBlank(ids)) {
            throw new BusinessException("获取删除转换执行信息失败，请稍后重试！");
        }
        List<String> list = JSONArray.parseArray(ids, String.class);
        jobRecordService.removeByIds(list);
        return ResultGenerator.getSuccessResult("删除转换执行信息成功！");
    }



    @YssPermissions(value = {"kettle:job:record"})
    @ApiOperation(value = "获取转换执行记录日志文件信息")
    @YssLog(module = "转换执行记录", remark = "获取转换执行记录日志文件信息")
    @PostMapping(value = "/viewTransRecordDetail")
    @ResponseBody
    public RestResult viewTransRecordDetail(String logFilePath) throws Exception {
        String logContent = FileUtils.readFileToString(new File(logFilePath), "UTF-8");
        logContent.replace("\r\n", "<br/>");
        return ResultGenerator.getSuccessResult("获取转换执行记录日志文件信息成功", logContent);
    }


    /**
     * 下载转换执行记录明细
     */
    @YssPermissions(value = {"kettle:job:record"})
    @ApiOperation(value = "下载转换执行记录明细")
    @YssLog(module = "转换执行记录", remark = "下载转换执行记录明细")
    @PostMapping(value = "/downloadTransRecord")
    @ResponseBody
    public RestResult downloadTransRecord(HttpServletResponse response, String logFilePath) throws Exception {
        // 查询文件路径
        FileWriterUtil.downloadFile(response, logFilePath);
        return ResultGenerator.getSuccessResult("下载转换执行记录明细成功");
    }

}
